package com.dom.common.utils;

import com.dom.common.constants.ConstantError;
import com.dom.common.model.standard.ErrorModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;

/**
 * 公共审核工具类
 */
public class CheckStatUtil {
    /*审核状态 00-待提交，01-待审核，02-审核通过，03-审核未通过，04-反审核*/
    public static final String STAT_VERFY_INIT = "00";
    public static final String STAT_VERFY_WAIT = "01";
    public static final String STAT_VERFY_PASS = "02";
    public static final String STAT_VERFY_FAIL = "03";
    public static final String STAT_VERFY_REVE = "04";
    /*撤销状态*/
    public static final String STAT_CANCEL_INIT = "10";//正常
    public static final String STAT_CANCEL_ING = "11";//撤销中
    public static final String STAT_CANCEL_OVER = "12";//已撤销
    
    /**
     * 审核判断
     * @param verfyStat
     * @param cancelStat
     * @param operatorId
     * @param makerId
     */
    public static ErrorModel checkAuditStat(String verfyStat, String cancelStat, String operatorId, String makerId){
        ErrorModel model = new ErrorModel();
        if (!STAT_VERFY_REVE.equals(verfyStat)){//非反审核状态
            if (STAT_VERFY_PASS.equals(verfyStat)){//审核通过状态
                model.setErrCodeAndErrMsg(ConstantError.RSP010100);
                return model;
            } else if (!STAT_VERFY_FAIL.equals(verfyStat)){//待审核状态
                model.setErrCodeAndErrMsg(ConstantError.RSP010099);
                return model;
            }
            if (!STAT_CANCEL_INIT.equals(cancelStat)){//只有正常状态的才审核
                model.setErrCodeAndErrMsg(ConstantError.RSP010101);
                return model;
            }
        }
        return model;
    }

    /**
     * 审核失败判断
     * @param verfyStat
     * @param cancelStat
     * @param operatorId
     * @param makerId
     */
    public static ErrorModel checkFailStat(String verfyStat, String cancelStat, String operatorId, String makerId,String errMsg){
        ErrorModel model = checkAuditStat(verfyStat,cancelStat,operatorId,makerId);
        if (model.isErr()){
            return model;
        }
        if(StringUtils.isEmpty(errMsg)){
            model.setErrCodeAndErrMsg(ConstantError.RSP010105);
        }
        return model;
    }

    /**
     * 提交审核判断
     * @param verfyStat
     * @param cancelStat
     * @param operatorId
     * @param makerId
     */
    public static ErrorModel checkPresentVerfy(String verfyStat, String cancelStat, String operatorId, String makerId){
        ErrorModel model = new ErrorModel();
        if (!STAT_VERFY_REVE.equals(verfyStat)){//非反审核状态
            if (STAT_VERFY_PASS.equals(verfyStat) || STAT_VERFY_WAIT.equals(verfyStat)){//审核通过状态
                model.setErrCodeAndErrMsg(ConstantError.RSP010102);
                return model;
            }
            if (!STAT_CANCEL_INIT.equals(cancelStat)){
                model.setErrCodeAndErrMsg(ConstantError.RSP010103);
                return model;
            }
            if (!operatorId.equals(makerId)){
                model.setErrCodeAndErrMsg(ConstantError.RSP010104);
                return model;
            }
        }
        return model;
    }

    /**
     *反审核判断
     * @param verfyStat
     * @param cancelStat
     * @param operatorId
     * @param verfyId
     */
    public static ErrorModel checkAntisenseVerfy(String verfyStat, String cancelStat, String operatorId, String verfyId){
        ErrorModel model = new ErrorModel();
        if (STAT_VERFY_REVE.equals(verfyStat)){//反审核状态
            model.setErrCodeAndErrMsg(ConstantError.RSP010095);
            return model;
        }
        if (!STAT_VERFY_PASS.equals(verfyStat)){//非审核通过状态
            model.setErrCodeAndErrMsg(ConstantError.UNPASSVERFYERROR);
            return model;
        }
        if(!verfyId.equals(operatorId)){//与审核人不一致不允许反审核
            model.setErrCodeAndErrMsg(ConstantError.UNSELFERROR);
            return model;
        }
        return model;
    }

    /**
     * 单据修改和删除，审核状态判断
     * @param verfyStat
     * @param cancelStat
     * @param operatorId
     * @param makerId
     */
    public static ErrorModel checkUpOrDelVerfy(String verfyStat, String cancelStat, String operatorId, String makerId){
        ErrorModel model = new ErrorModel();
        if (!STAT_VERFY_REVE.equals(verfyStat)){//非反审核状态
            if (STAT_VERFY_WAIT.equals(verfyStat)){//待审核状态
                model.setErrCodeAndErrMsg(ConstantError.RSP010096);
                return model;
            } else if (STAT_VERFY_PASS.equals(verfyStat)){//审核通过状态
                model.setErrCodeAndErrMsg(ConstantError.RSP010097);
                return model;
            }
            if (!STAT_CANCEL_INIT.equals(cancelStat)){//非正常状态不允许变更
                model.setErrCodeAndErrMsg(ConstantError.RSP010098);
                return model;
            }
            if(!makerId.equals(operatorId)){//非本人不允许变更
                model.setErrCodeAndErrMsg(ConstantError.UNSELFERROR);
                return model;
            }
        }
        return model;
    }
}
